//
//  UILabel+Helper.m
//  Kaagad Peso
//
//  Created by ZhengChengFeng on 2024/10/15.
//

#import "UILabel+Helper.h"

@implementation UILabel (Helper)

+ (UILabel *)createLabel:(UIFont *)textFont color:(UIColor *)textColor{
    UILabel *label = [[UILabel alloc] init];
    label.textColor = textColor;
    label.backgroundColor = [UIColor clearColor];
    label.font = textFont;
    return label;
}


- (void)addAttributes:(NSDictionary *)attributes forString:(NSString *)string{
    if (self.text.length == 0) {
        return;
    }
    NSMutableAttributedString * attributedString = [[NSMutableAttributedString alloc] initWithString:self.text];
    NSString * temp = nil;
    for(int i = 0; i < self.text.length; i++)
    {
        if (i + string.length > self.text.length) {
            break;
        }
        temp = [self.text substringWithRange:NSMakeRange(i, string.length)];
        if ([temp isEqualToString:string]) {
            [attributedString addAttributes:attributes range:NSMakeRange(i, string.length)];
        }
    }
    self.attributedText = attributedString;
}

- (void)addAttributes:(NSDictionary *)attributes forStringArray:(NSArray *)stringArray{
    NSMutableAttributedString * attributedString = [[NSMutableAttributedString alloc] initWithString:self.text];
    for (int i = 0; i < stringArray.count; i++) {
        NSString * string = stringArray[i];
        NSString * temp = nil;
        for(int j = 0; j < self.text.length; j++)
        {
            if (j + string.length > self.text.length) {
                break;
            }
            temp = [self.text substringWithRange:NSMakeRange(j, string.length)];
            if ([temp isEqualToString:string]) {
                [attributedString addAttributes:attributes range:NSMakeRange(j, string.length)];
            }
        }
    }
    self.attributedText = attributedString;
}

- (void)addAttributesArray:(NSArray *)attributesArray forStringArray:(NSArray *)stringArray{
    if (attributesArray.count != stringArray.count) {
        return;
    }

    NSMutableAttributedString * attributedString = [[NSMutableAttributedString alloc] initWithString:self.text];
    for (int i = 0; i < stringArray.count; i++) {
        NSString * string = stringArray[i];
        NSDictionary * attributes = attributesArray[i];
        NSString * temp = nil;
        for(int j = 0; j < self.text.length; j++)
        {
            if (j + string.length > self.text.length) {
                break;
            }
            temp = [self.text substringWithRange:NSMakeRange(j, string.length)];
            if ([temp isEqualToString:string]) {
                [attributedString addAttributes:attributes range:NSMakeRange(j, string.length)];
            }
        }
    }
    self.attributedText = attributedString;
}

@end

